package com.egg.auth.controller.web;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.egg.auth.entity.job.Job;
import com.egg.auth.entity.job.JobLinkUser;
import com.egg.auth.entity.user.User;
import com.egg.auth.enums.job.JobAuthCodeEnum;
import com.egg.auth.model.request.web.JobPostOrPut;
import com.egg.auth.model.response.web.DeptPage;
import com.egg.auth.model.response.web.JobInfo;
import com.egg.auth.model.response.web.JobPage;
import com.egg.auth.model.response.web.UserPage;
import com.egg.auth.service.job.IJobLinkUserService;
import com.egg.auth.service.job.IJobService;
import com.egg.auth.service.user.IUserService;
import com.egg.common.core.model.controller.WebController;
import com.egg.common.core.model.response.OptionsResponse;
import com.egg.common.core.model.response.PageData;
import com.egg.common.core.model.response.ResponseData;
import com.egg.common.util.JsonUtil;
import com.egg.common.util.OptionalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author east
 */
@Slf4j
@Api(tags = WebController.API_PREFIX + "岗位中心")
@RestController
@RequestMapping("/job")
public class WebJobController extends WebController {


    @Autowired
    private IJobService jobService;
    @Autowired
    private IJobLinkUserService jobLinkUserService;
    @Autowired
    private IUserService userService;


    @ApiOperation(value = "岗位编码类型集合")
    @GetMapping(value = "/code/list")
    public ResponseData<List<OptionsResponse>> roleCodeList() {
        List<JobAuthCodeEnum> values = Arrays.stream(JobAuthCodeEnum.values()).collect(Collectors.toList());
        List<OptionsResponse> optionList = new ArrayList<>();
        values.forEach(o -> {
            OptionsResponse option = new OptionsResponse();
            option.setValue(o.getCode());
            option.setLabel(o.getMessage());
            optionList.add(option);
        });
        return ResponseData.success(optionList);
    }

    @ApiOperation(value = "岗位新增")
    @PostMapping
    public ResponseData<Long> jobPost(@RequestBody JobPostOrPut param) {
        Long id = jobService.add(param);
        return ResponseData.success(id);
    }

    @ApiOperation(value = "岗位详情")
    @GetMapping("/{id}/info")
    public ResponseData<JobInfo> jobInfoGet(@ApiParam(value = "岗位ID") @PathVariable String id) {
        Job job = jobService.baseGetById(id);
        JobInfo jobInfo = JsonUtil.objToObj(job, JobInfo.class);
        return ResponseData.success(jobInfo);
    }

    @ApiOperation(value = "岗位编辑")
    @PutMapping("/{id}")
    public ResponseData<String> jobPut(@PathVariable String id, @RequestBody JobPostOrPut param) {
        jobService.put(id, param);
        return ResponseData.success(id);
    }

    @ApiOperation(value = "岗位分页")
    @GetMapping("/page")
    public ResponseData<PageData<DeptPage>> jobPageGet(
            @ApiParam(value = "页数", required = true) @RequestParam(value = "current", defaultValue = "1") Integer current
            , @ApiParam(value = "页长", required = true) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
            , @ApiParam(value = "岗位名称") @RequestParam(value = "name", required = false) String name
    ) {
        LambdaQueryWrapper<Job> qw = jobService.buildLambdaQw();
        qw.orderByAsc(Job::getSort);
        if (ObjectUtil.isNotEmpty(name)) {
            qw.like(Job::getName, name);
        }
        Page<Job> dbPage = jobService.page(new Page<>(current, pageSize), qw);
        if (ObjectUtil.isEmpty(dbPage.getRecords())) {
            return ResponseData.success(new PageData<>(dbPage));
        }
        List<JobPage> pageList = JsonUtil.listToList(dbPage.getRecords(), JobPage.class);
        return ResponseData.success(new PageData<>(dbPage, pageList));
    }

    @ApiOperation(value = "部门删除")
    @DeleteMapping
    public ResponseData<String> jobListDel(@RequestParam Collection<String> ids) {
        jobService.del(
                ids.stream()
                        .filter(o -> ObjectUtil.isNotEmpty(o))
                        .map(o -> Long.valueOf(o))
                        .collect(Collectors.toSet())
        );
        return ResponseData.success();
    }

    @ApiOperation(value = "岗位新增用户")
    @PostMapping("/{id}/user")
    public ResponseData<String> jobUserPost(
            @ApiParam(value = "岗位ID", required = true) @PathVariable String id
            , @ApiParam(value = "用户ID集合") @RequestParam Collection<String> userIds
    ) {
        jobLinkUserService.add(
                Long.valueOf(id)
                , userIds.stream()
                        .filter(o -> ObjectUtil.isNotEmpty(o))
                        .map(o -> Long.valueOf(o))
                        .collect(Collectors.toSet())
        );
        return ResponseData.success();
    }

    @ApiOperation(value = "岗位覆盖用户")
    @PutMapping("/{id}/user")
    public ResponseData<String> jobUserPut(
            @ApiParam(value = "岗位ID", required = true) @PathVariable String id
            , @ApiParam(value = "用户ID集合") @RequestParam Collection<String> userIds
    ) {
        jobLinkUserService.put(
                Long.valueOf(id)
                , userIds.stream()
                        .filter(o -> ObjectUtil.isNotEmpty(o))
                        .map(o -> Long.valueOf(o))
                        .collect(Collectors.toSet())
        );
        return ResponseData.success();
    }

    @ApiOperation(value = "岗位用户分页")
    @GetMapping("/{id}/user/page")
    public ResponseData<PageData<UserPage>> jobUserPageGet(
            @ApiParam(value = "岗位ID", required = true) @PathVariable String id
            , @ApiParam(value = "页数", required = true) @RequestParam(value = "current", defaultValue = "1") Integer current
            , @ApiParam(value = "页长", required = true) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        Page<JobLinkUser> dbPage = jobLinkUserService.page(
                current, pageSize
                , OptionalUtil.build(id)
        );
        List<User> userList = userService.baseFindById(dbPage.getRecords().stream().map(o -> o.getUserId()).collect(Collectors.toList()));
        return ResponseData.success(new PageData<>(dbPage, userService.convertUserPage(userList)));
    }


}

